/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 * File: platform.h
 *************************************************************************
 *\author: starlove
 *************************************************************************/

#ifndef __MLSE_LIBRARY_PLATFORM_H__
#define __MLSE_LIBRARY_PLATFORM_H__

#include <map>
#include <vector>
#include <string>

///!!! just support for python(stackless) and erlang
//#define __MLSE_SCRIPT_PARALLEL__

#define MASK_TEST( val, mask ) \
	((val & mask) == mask)

#ifdef _COMPILER_GCC_
#include <unistd.h>
#ifdef __MLSE_USE_CLASSIC_HASH_MAP__
#include <ext/hash_map>
namespace stdext
{
using namespace __gnu_cxx;
}
#include <hash_fun.h>
struct str_hash
{
    size_t operator()(const std::string& str) const
    {
        return stdext::__stl_hash_string(str.c_str());
    }
};
#else	//__MLSE_USE_CLASSIC_HASH_MAP__
#include <unordered_map>
#endif	//__MLSE_USE_CLASSIC_HASH_MAP__
#else	//_COMPILER_GCC_
#include <hash_map>
#define usleep(t) Sleep(t / 1000)
#endif	//_COMPILER_GCC_

#ifdef _MSC_VER
typedef __int64 INT64;
typedef unsigned __int64 UINT64;
#else
#include <sys/types.h>
typedef int64_t INT64;
#endif
extern unsigned long GetTimestamp();

#define __USE_MLSE_NETWORK_LIB__

#ifdef __USE_MLSE_NETWORK_LIB__

#define __MLSE_EVENT_LIMIT__ 1000				/// Event lookup-queue limit
#define __MLSE_COMPRESS_MIN_LENGTH__ 128		/// not compress data-length limit
#define __MLSE_COMPRESS_ENABLED__				///

#ifdef __MLSE_PLATFORM_WIN__

#include <winsock2.h>
#include <ws2tcpip.h>
#include <mswsock.h>
#include <windows.h>

#ifndef _MSC_VER
typedef
BOOL
(PASCAL FAR * LPFN_ACCEPTEX)(
    IN SOCKET sListenSocket,
    IN SOCKET sAcceptSocket,
    IN PVOID lpOutputBuffer,
    IN DWORD dwReceiveDataLength,
    IN DWORD dwLocalAddressLength,
    IN DWORD dwRemoteAddressLength,
    OUT LPDWORD lpdwBytesReceived,
    IN LPOVERLAPPED lpOverlapped
	);

#define WSAID_ACCEPTEX \
        {0xb5367df1,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
#endif // _MSC_VER
typedef void *EVHANDLE;

#else // __MLSE_PLATFORM_WIN__

#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>

typedef int	SOCKET;
typedef int HANDLE;
typedef int EVHANDLE;

#ifndef INVALID_SOCKET
#define INVALID_SOCKET  (SOCKET)(~0)
#endif // INVALID_SOCKET

#define INFINITE 0xFFFFFFFF

#define __MLSE_WORK_THREAD_NUM__ 		4
#define __MLSE_WORK_THREAD_EVENT_NUM__ 	100
#define __MLSE_EPOLL_RECV_BUFFSIZE__ 	512
#define __MLSE_WORK_THREAD_FRAME__ 		30

#endif // __MLSE_PLATFORM_WIN__

#endif // __MLSE_NETWORK_LIB_USE__

#endif //
